package trb.complexshield.bool;

import no.hrp.hvrc.rad.calc.intersect.Segment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OrSegments implements SegmentsProducer {

    private final List<SegmentsProducer> list = new ArrayList();

    public OrSegments(List<SegmentsProducer> producers) {
        this.list.addAll(producers);
    }

    @Override
    public Segments getSegments(Segment line, Segments out, SegmentsMemory memory) {
        out.clear();
        Segments[] segmentsList = new Segments[list.size()];
        for (int i=0; i<list.size(); i++) {
            SegmentsProducer producer = list.get(i);
            Segments segments = memory.newSegments();
            producer.getSegments(line, segments, memory);
            segmentsList[i] = segments;
        }
        Segments.merge(segmentsList, out);
        return out;
    }

    public static List<SegmentsProducer> toList(SegmentsProducer... producers) {
        return new ArrayList(Arrays.asList(producers));
    }
}
